﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Globalization;
using InSTEDD.Labs.Geo.Properties;

namespace InSTEDD.Labs.Geo
{
    public class DistanceHelper
    {
        public static string GetDistanceString(double? distanceInKm)
        {
            if (distanceInKm.HasValue)
            {
                double distanceInKmTrunc;
                if (distanceInKm.Value != 0)
                {
                    double exponent = (Math.Floor(Math.Log10(distanceInKm.Value))) - 2.0;
                    distanceInKmTrunc = Math.Floor(distanceInKm.Value / Math.Pow(10.0, exponent));
                    distanceInKmTrunc = distanceInKmTrunc * Math.Pow(10.0, exponent);
                }
                else
                {
                    distanceInKmTrunc = 0;
                }
                return string.Format(CultureInfo.InvariantCulture, Resources.FormatKmDistance, distanceInKmTrunc);
            }
            else
            {
                return "unkown";
            }
        }
    }
}
